package com.zsj.Tree;

/**
 * @Author zsj
 * @Version 1.0
 * @Date 2024/4/13 14:55
 * @Description 对称二叉树
 */
public class SymmetryTree {


    /**
     * 检查一棵树是否为对称二叉树
     *
     * @param node 树对象
     * @return true对称 false不对称
     */
    public boolean isSymmetry(TreeNode node) {
        return check(node.left, node.right);
    }


    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (right == null || left == null) return false;
        if (left.val != right.val) return false;
        //对比的是左子树左边要等于右子树的右边  左子树的右边要等于右子树的左边
        return check(left.left, right.right)
               && check(left.right, right.left);
    }
}
